/*
 * @Author: zhousg
 * @Date: 2020-09-23 15:24:20
 * @Last Modified by: zhousg
 * @Last Modified time: 2021-01-05 11:09:32
 */

import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

// 路由组件
const Layout = () => import('@/views/Layout')
const Login = () => import('@/views/login/index')
const LoginCallback = () => import('@/views/login/callback')
const Register = () => import('@/views/login/register')
const Forget = () => import('@/views/login/forget')
const Home = () => import('@/views/home/index')
const New = () => import('@/views/home/new')
const One = () => import('@/views/home/one')
const Vogue = () => import('@/views/home/vogue')
const Download = () => import('@/views/home/download')
const Category = () => import('@/views/category/index')
const CategorySub = () => import('@/views/category/sub')
const Search = () => import('@/views/category/search')
const Goods = () => import('@/views/goods/index')
const Cart = () => import('@/views/goods/cart')
const BrandList = () => import('@/views/brand/list')
const BrandItem = () => import('@/views/brand/item')
const Same = () => import('@/views/goods/same')

const MemberLayout = () => import('@/views/member/Layout')
const MemberHome = () => import('@/views/member/home')
const MemberInfo = () => import('@/views/member/info')
const MemberSafe = () => import('@/views/member/safe')
const MemberSafeMobile = () => import('@/views/member/safe/mobile')
const MemberAddress = () => import('@/views/member/address')
const MemberFoot = () => import('@/views/member/foot')
const MemberCollectGoods = () => import('@/views/member/collect/goods')
const MemberCollectSpecial = () => import('@/views/member/collect/special')
const MemberCollectBrand = () => import('@/views/member/collect/brand')
const MemberOrder = () => import('@/views/member/order')
const MemberOrderDetail = () => import('@/views/member/order/detail')
const MemberComment = () => import('@/views/member/comment')
const MemberCommentPublish = () => import('@/views/member/comment/publish')

const CheckOut = () => import('@/views/member/pay/checkout')
const Pay = () => import('@/views/member/pay/index')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      { path: '/', component: Home },
      { path: '/new', component: New },
      { path: '/one', component: One },
      { path: '/vogue', component: Vogue },
      { path: '/download', component: Download },
      { path: '/category/:id', component: Category },
      { path: '/category/sub/:id', component: CategorySub },
      { path: '/search', component: Search },
      { path: '/product/:id', component: Goods },
      { path: '/brand', component: BrandList },
      { path: '/brand/:id', component: BrandItem },
      { path: '/same/:id', component: Same },
      { path: '/cart', component: Cart },
      { path: '/register', component: Register },
      { path: '/forget', component: Forget },
      { path: '/member/comment/:id', component: MemberCommentPublish },
      { path: '/member/checkout', component: CheckOut },
      { path: '/member/pay', component: Pay },
      {
        path: '/member',
        component: MemberLayout,
        children: [
          { path: '/member', component: MemberHome },
          { path: '/member/info', component: MemberInfo },
          { path: '/member/safe', component: MemberSafe },
          { path: '/member/safe/mobile', component: MemberSafeMobile },
          { path: '/member/address', component: MemberAddress },
          { path: '/member/foot', component: MemberFoot },
          { path: '/member/collect/goods', component: MemberCollectGoods },
          { path: '/member/collect/special', component: MemberCollectSpecial },
          { path: '/member/collect/brand', component: MemberCollectBrand },
          { path: '/member/order', component: MemberOrder },
          { path: '/member/order/:id', component: MemberOrderDetail },
          { path: '/member/comment', component: MemberComment }
        ]
      }
    ]
  },
  { path: '/login', component: Login },
  { path: '/login/callback', component: LoginCallback }
]

const router = new VueRouter({
  routes,
  scrollBehavior (to, from, savedPosition) {
    return { x: 0, y: 0 }
  }
  // scrollBehavior (to, from, savedPosition) {
  //   if (to.hash) {
  //     return {
  //       selector: to.hash
  //     }
  //   }
  // }
})

// 约定 /member 开头的路由地址需要登录访问
router.beforeEach((to, from, next) => {
  const { token } = store.state.user
  const returnRoute = { path: '/login', query: { returnUrl: to.fullPath } }
  if (to.path.startsWith('/member') && !token) return next(returnRoute)
  next()
})

export default router
